package com.fu.reflect1;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author 付大大
 * @description
 * @create 2021-01-17 12:34
 */
public class Test2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//        method1();

//        method2();

//        method3();

        Class<?> aClass = Class.forName("com.fu.reflect1.Student");

        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class);

        declaredConstructor.setAccessible(true);

        Student student = (Student) declaredConstructor.newInstance("张三");

        System.out.println(student);

    }

    private static void method3() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class<?> aClass = Class.forName("com.fu.reflect1.Student");
        Student o = (Student) aClass.newInstance();
        System.out.println(o);
    }

    private static void method2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class<?> aClass = Class.forName("com.fu.reflect1.Student");
        Constructor<?> constructor = aClass.getConstructor();

        Student o = (Student) constructor.newInstance();
        System.out.println(o);
    }

    private static void method1() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class<?> aClass = Class.forName("com.fu.reflect1.Student");

        Constructor<?> constructor = aClass.getConstructor(String.class, int.class);

        Student student = (Student) constructor.newInstance("zhangsan", 23);

        System.out.println(student);
    }
}
